using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Train : MonoBehaviour
{
    public enum TrainType
    {
        Head,
        Body,
        Other
    }
    public delegate void OnTrainArrived();
    public bool isRight;
    public float speed = 5;

    private OnTrainArrived callback;
    private bool moving = false;
    private float targetX;

    public void MoveTo(float x, OnTrainArrived cb)
    {
        if (!moving)
        {
            if(isRight ? transform.position.x < x : transform.position.x > x)
            {
                moving = true;
                targetX = x;
                callback = cb;
            }
        }
    }

    public void Update()
    {
        if (moving)
        {
            if (isRight ? transform.position.x < targetX : transform.position.x > targetX)
            {
                transform.position += new Vector3(isRight ? 1 : -1, 0, 0) * speed * Time.deltaTime;
            }
            else
            {
                moving = false;
                callback?.Invoke();
            }
        }
    }
}
